﻿using System;
using System.Drawing;
using System.Collections.Generic;
using SlimDX.Direct3D11;


namespace InsTron
{
    enum AaMethod
    {
        None,
        SSAAx256,
        SSAAxP112,
        SSAAxP113,
        SSAAxP142,
        SSAAxP143,
        QCAA,
        EQCAA,
        MSAAx4,
        MSAAx8,
        CSAAx4c16,
        CSAAx8c32,
        SBAAx4,
        SBAAx8
    }


    static class Settings
    {
        public static void DebugProfile()
        {
            CurrentResolutionKey = "AA_TESTING_QUALITY";
            Method = AaMethod.SSAAx256;
            PrepareAa();
        }


        public static bool PlayerInitialized { get; set; }
        private static Dictionary<string, Size> resolutionSet;
        public static RasterizerStateDescription RasteriserDesc { get; private set; }
        public static string CurrentResolutionKey { get; private set; }
        public static BikeParameters[] BikeParameters { get; private set; }
        public static bool Fullscreen { get; private set; }
        public static bool PlatformReflection { get; set; }
        public static bool MipMapping { get; set; }
        public static short SupersamplingFactor { get; private set; }
        public static short MsaaSamples { get; private set; }
        public static short CoverageSamples { get; private set; }
        public static bool QcaaEnabled { get; private set; }
        public static bool EqcaaEnabled { get; private set; }
        public static bool P112Enabled { get; private set; }
        public static bool P113Enabled { get; private set; }
        public static bool P142Enabled { get; private set; }
        public static bool P143Enabled { get; private set; }
        public static bool SbaaEnabled { get; private set; }

        public static AaMethod Method { get; private set; }

        public static void PrepareAa()
        {
            switch (Method)
            {
                case AaMethod.None:
                    break;
                case AaMethod.SSAAx256:
                    SupersamplingFactor = 16;
                    break;
                case AaMethod.SSAAxP112:
                    P112Enabled = true;
                    break;
                case AaMethod.SSAAxP113:
                    P113Enabled = true;
                    break;
                case AaMethod.SSAAxP142:
                    P142Enabled = true;
                    break;
                case AaMethod.SSAAxP143:
                    P143Enabled = true;
                    break;
                case AaMethod.QCAA:
                    QcaaEnabled = true;
                    break;
                case AaMethod.EQCAA:
                    EqcaaEnabled = true;
                    break;
                case AaMethod.MSAAx4:
                    MsaaSamples = 4;
                    CoverageSamples = 4;
                    break;
                case AaMethod.MSAAx8:
                    MsaaSamples = 8;
                    CoverageSamples = 8;
                    break;
                case AaMethod.CSAAx4c16:
                    MsaaSamples = 4;
                    CoverageSamples = 16;
                    break;
                case AaMethod.CSAAx8c32:
                    MsaaSamples = 4;
                    CoverageSamples = 32;
                    break;
                case AaMethod.SBAAx4:
                    MsaaSamples = 4;
                    CoverageSamples = 4;
                    SbaaEnabled = true;
                    break;
                case AaMethod.SBAAx8:
                    MsaaSamples = 8;
                    CoverageSamples = 8;
                    SbaaEnabled = true;
                    break;
            }
        }


        public static void Initialize()
        {
            PlayerInitialized = false;
            InitializeRasterizerDescForMsaa();
            PrepareResoultionSet();
            CurrentResolutionKey = "1280x960";
            Fullscreen = false;
            PlatformReflection = false;
            MipMapping = false;
            SupersamplingFactor = 1;
            QcaaEnabled = false;
            EqcaaEnabled = false;
            P112Enabled = false;
            P113Enabled = false;
            P142Enabled = false;
            P143Enabled = false;
            MsaaSamples = 1;
            CoverageSamples = 0;
            SbaaEnabled = false;

            InitializeBikeParameters();
        }


        private static void InitializeBikeParameters()
        {
            BikeParameters = new BikeParameters[2];
            BikeParameters[0] = new BikeParameters
            {
                Color = Color.DarkRed
            };
            BikeParameters[1] = new BikeParameters
            {
                Color = Color.Blue
            };
        }


        private static void PrepareResoultionSet()
        {
            resolutionSet = new Dictionary<string, Size>();
            resolutionSet.Add("AA_TESTING_QUALITY", new Size(700, 800)); // AA_TESTING
            resolutionSet.Add("AA_TESTING_PERFORMANCE", new Size(3200, 4000)); // AA_TESTING
            resolutionSet.Add("1280x960", new Size(1280, 960)); // 4/3
            resolutionSet.Add("1280x800", new Size(1280, 800)); // 16/10
            resolutionSet.Add("1280x720", new Size(1280, 720)); // 16/9 HD
            resolutionSet.Add("1920x1080", new Size(1920, 1080)); // 16/9 fullHD
            resolutionSet.Add("1366x768", new Size(1366, 768)); // 16/9 laptop
        }


        public static Size CurrentResolution
        {
            get
            {
                try
                {
                    return resolutionSet[CurrentResolutionKey];
                }
                catch (KeyNotFoundException)
                {
                    return new Size(1280, 960);
                }
            }
        }


        public static Size WorkingResolution
        {
            get
            {
                return new Size(CurrentResolution.Width * SupersamplingFactor,
                    CurrentResolution.Height * SupersamplingFactor);
            }
        }


        public static void HdProfile()
        {
            CurrentResolutionKey = "1920x1080";
            Fullscreen = true;
            SupersamplingFactor = 2;
        }


        public static void LaptopProfile()
        {
            CurrentResolutionKey = "1366x768";
            Fullscreen = true;
            SupersamplingFactor = 2;
        }


        public static List<string> GetResolutionKeysList()
        {
            return new List<string>(resolutionSet.Keys);
        }


        public static void CommitSettingsChange(bool fullscreen, string resolutionKey, bool supersampling, Color player1Color, Color player2Color, bool musicEnabled)
        {
            CommitGraphicsSettingsChange(fullscreen, resolutionKey, supersampling);
            CommitBikesParametersChange(player1Color, player2Color);
        }


        private static void CommitGraphicsSettingsChange(bool fullscreen, string resolutionKey, bool supersampling)
        {
            bool updateNeeded = !resolutionKey.Equals(CurrentResolutionKey) || fullscreen != Fullscreen;

            Fullscreen = fullscreen;
            CurrentResolutionKey = resolutionKey;
            SupersamplingFactor = (short)(supersampling ? 2 : 1);

            if (updateNeeded)
            {
                Program.UpdateRenderSettings();
            }
        }


        private static void CommitBikesParametersChange(Color player1Color, Color player2Color)
        {
            BikeParameters[0].Color = player1Color;
            BikeParameters[1].Color = player2Color;
        }

        
        private static void InitializeRasterizerDescForMsaa()
        {
 	        RasteriserDesc = new RasterizerStateDescription()
            {
                CullMode = CullMode.Back,
                DepthBias = 0,
                DepthBiasClamp = 0,
                FillMode = FillMode.Solid,
                IsAntialiasedLineEnabled = true,
                IsDepthClipEnabled = false,
                IsFrontCounterclockwise = false,
                IsMultisampleEnabled = true,
                IsScissorEnabled = false,
                SlopeScaledDepthBias = 0
            };
        }
    }
}
